package com.swiftride.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.json.JSONUtil;
import com.swiftride.properties.SaTokenStpProperties;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
// 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    private final RedisTemplate<String, Object> redisTemplate;

    private final SaTokenStpProperties saTokenStpProperties;

    public StpInterfaceImpl(RedisTemplate<String, Object> redisTemplate, SaTokenStpProperties saTokenStpProperties) {
        this.redisTemplate = redisTemplate;
        this.saTokenStpProperties = saTokenStpProperties;
    }

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String key = String.format(saTokenStpProperties.getPermissionKey(), loginId);
        Object permissionListObject = redisTemplate.opsForValue().get(key);
        List<String> permissionList = JSONUtil.toList(JSONUtil.parseArray(permissionListObject), String.class);
        return permissionList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return null;
    }

}
